package com.zhongan.gateway.common.utils;

import java.text.NumberFormat;

import com.alibaba.fastjson.JSONObject;

/**
 * @author
 */
public class TypeUtil {

    public static String obj2Str(Object obj) {
        String s = "";
        if (obj != null) {
            if (obj instanceof String) {
                s = (String) obj;
            } else {
                s = JSONObject.toJSONString(obj);
                if (s != null && s.contains("\"class\"")) {
                    s = s.replaceAll(",\"class\":\".+?\"", "").replaceAll("\"class\":\".+?\",", "")
                            .replaceAll("\"class\":\".+?\"", "");
                }
            }
        }
        return s;
    }

    public static String initDsfStr(String dsfStr) {
        String s = dsfStr;
        if (s != null && s.contains("@type")) {
            s = s.replaceAll("\"@type\":\".+?\",", "").replaceAll("\"@type\":\".+?\"", "");
        }
        return s;
    }

    //    public static void main(String[] args) {
    //        String s = "[{\"class\":\"com.example.client.bean.User\"}]";
    //        String s2 = "[{\"class\":\"com.example.client.bean.User\",\"name\":\"test\",\"age\":1,\"class\":\"com.example.client.bean.User\"}]";
    //        System.out.println(s.replaceAll("\"class\":\".+?\"", ""));
    //        System.out.println(s2.replaceAll(",\"class\":\".+?\"", "").replaceAll("\"class\":\".+?\",", ""));
    //    }

    public static String percent(int top, int bottom) {
        String baifenbi = "";// 接受百分比的值  
        double baiy = top * 1.0;
        double baiz = bottom * 1.0;
        double fen = baiy / baiz;
        NumberFormat nf = NumberFormat.getPercentInstance(); //注释掉的也是一种方法  
        nf.setMinimumFractionDigits(2);// 保留到小数点后几位  
        //        DecimalFormat df1 = new DecimalFormat("##.00%"); // ##.00%  
        // 百分比格式，后面不足2位的用0补齐  
        baifenbi = nf.format(fen);
        //        baifenbi = df1.format(fen);

        return baifenbi;
    }

    public static int divide(int top, int bottom) {
        double baiy = top * 1.0;
        double baiz = bottom * 1.0;
        double fen = baiy / baiz;
        return (int) Math.rint(fen);
    }

}
